<!--
  智慧社区管理系统 - 设施管理
  @author Wu.Liang
  @date 2025-01-20
  @description 设施管理页面，展示社区公共设施台账、使用状态、容量管理等功能
-->
<template>
  <div class="facility-management-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2 class="page-title">
          <el-icon class="title-icon"><House /></el-icon>
          设施管理
        </h2>
        <p class="page-description">管理社区公共设施台账，监控使用状态，优化设施配置</p>
      </div>
      <div class="header-right">
        <el-button type="primary" :icon="Plus" @click="handleAddFacility">新增设施</el-button>
        <el-button type="success" :icon="DataBoard" @click="handleFacilityStats">设施统计</el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon total">
                <el-icon><House /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ facilityStats.totalCount }}</div>
                <div class="stats-label">设施总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon normal">
                <el-icon><Select /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ facilityStats.normalCount }}</div>
                <div class="stats-label">正常使用</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon maintenance">
                <el-icon><Tools /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ facilityStats.maintenanceCount }}</div>
                <div class="stats-label">维护中</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stats-card" shadow="hover">
            <div class="stats-content">
              <div class="stats-icon disabled">
                <el-icon><CloseBold /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ facilityStats.disabledCount }}</div>
                <div class="stats-label">已停用</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索和筛选 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="searchForm" inline>
        <el-form-item label="设施类型">
          <el-select v-model="searchForm.facilityType" placeholder="请选择设施类型" clearable style="width: 150px">
            <el-option label="健身设施" :value="1" />
            <el-option label="娱乐设施" :value="2" />
            <el-option label="停车设施" :value="3" />
            <el-option label="绿化设施" :value="4" />
            <el-option label="安全设施" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 150px">
            <el-option label="正常使用" :value="1" />
            <el-option label="维护中" :value="2" />
            <el-option label="已停用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="设施位置">
          <el-input v-model="searchForm.location" placeholder="请输入设施位置" style="width: 200px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">搜索</el-button>
          <el-button :icon="Refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 设施列表 -->
    <el-card class="facility-list-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="card-title">设施台账列表</span>
          <div class="header-actions">
            <el-button :icon="Download" @click="handleExport">导出数据</el-button>
            <el-button :icon="Setting" @click="handleSettings">设施设置</el-button>
          </div>
        </div>
      </template>

      <el-table 
        :data="facilityList" 
        v-loading="loading" 
        style="width: 100%"
        :row-class-name="getRowClassName"
      >
        <el-table-column prop="facilityName" label="设施名称" min-width="150">
          <template #default="{ row }">
            <div class="facility-name">
              <el-icon class="facility-icon" :class="getFacilityIconClass(row.facilityType)">
                <component :is="getFacilityIcon(row.facilityType)" />
              </el-icon>
              <span>{{ row.facilityName }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="facilityType" label="设施类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getFacilityTypeTagType(row.facilityType)" size="small">
              {{ getFacilityTypeName(row.facilityType) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="location" label="设施位置" min-width="150" />
        
        <el-table-column prop="status" label="使用状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)" size="small">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="capacity" label="容量" width="100">
          <template #default="{ row }">
            <span v-if="row.capacity">{{ row.capacity }}人</span>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="currentUsage" label="当前使用" width="100">
          <template #default="{ row }">
            <span v-if="row.currentUsage !== undefined">{{ row.currentUsage }}人</span>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="usageRate" label="使用率" width="120">
          <template #default="{ row }">
            <el-progress 
              v-if="row.usageRate !== undefined"
              :percentage="row.usageRate" 
              :color="getUsageRateColor(row.usageRate)"
              :stroke-width="8"
              :show-text="false"
            />
            <span v-if="row.usageRate !== undefined" class="usage-rate-text">{{ row.usageRate }}%</span>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="lastMaintenance" label="最后维护时间" width="180" />
        
        <el-table-column prop="nextMaintenance" label="下次维护时间" width="180">
          <template #default="{ row }">
            <span v-if="row.nextMaintenance">{{ row.nextMaintenance }}</span>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="handleDetail(row)">详情</el-button>
            <el-button size="small" type="warning" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="success" @click="handleMaintenance(row)">维护</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 设施详情弹窗 -->
    <el-dialog 
      v-model="detailDialogVisible" 
      title="设施详情" 
      width="800px"
      :close-on-click-modal="false"
    >
      <div class="facility-detail" v-if="currentFacility">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="设施名称">{{ currentFacility.facilityName }}</el-descriptions-item>
          <el-descriptions-item label="设施类型">{{ getFacilityTypeName(currentFacility.facilityType) }}</el-descriptions-item>
          <el-descriptions-item label="设施编号">{{ currentFacility.facilityCode }}</el-descriptions-item>
          <el-descriptions-item label="设施位置">{{ currentFacility.location }}</el-descriptions-item>
          <el-descriptions-item label="使用状态">
            <el-tag :type="getStatusTagType(currentFacility.status)">
              {{ getStatusName(currentFacility.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="容量">{{ currentFacility.capacity || '-' }}人</el-descriptions-item>
          <el-descriptions-item label="当前使用">{{ currentFacility.currentUsage || '-' }}人</el-descriptions-item>
          <el-descriptions-item label="使用率">
            <span v-if="currentFacility.usageRate !== undefined">{{ currentFacility.usageRate }}%</span>
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="最后维护时间">{{ currentFacility.lastMaintenance }}</el-descriptions-item>
          <el-descriptions-item label="下次维护时间">
            <span v-if="currentFacility.nextMaintenance">{{ currentFacility.nextMaintenance }}</span>
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="设施描述" :span="2">{{ currentFacility.description || '暂无描述' }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleEditFacility">编辑设施</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { 
  House, 
  Plus, 
  DataBoard, 
  Select, 
  Tools, 
  CloseBold,
  Search,
  Refresh,
  Download,
  Setting,
  VideoCamera,
  Lock,
  Sunny,
  ArrowUp,
  CircleClose
} from '@element-plus/icons-vue'
import { showMessage } from '@/utils/message'

// 响应式数据
const loading = ref(false)
const detailDialogVisible = ref(false)
const currentFacility = ref<any>(null)

// 搜索表单
const searchForm = reactive({
  facilityType: undefined as number | undefined,
  status: undefined as number | undefined,
  location: ''
})

// 分页信息
const pagination = reactive({
  current: 1,
  size: 20,
  total: 0
})

// 设施统计数据
const facilityStats = reactive({
  totalCount: 45,
  normalCount: 38,
  maintenanceCount: 5,
  disabledCount: 2
})

// 设施列表数据
const facilityList = ref([
  {
    id: 1,
    facilityName: '社区健身中心',
    facilityCode: 'JS001',
    facilityType: 1,
    location: '社区中心广场',
    status: 1,
    capacity: 50,
    currentUsage: 12,
    usageRate: 24,
    lastMaintenance: '2025-01-15',
    nextMaintenance: '2025-02-15',
    description: '社区健身中心，配备跑步机、健身器材等设备'
  },
  {
    id: 2,
    facilityName: '儿童游乐场',
    facilityCode: 'YL002',
    facilityType: 2,
    location: '社区花园',
    status: 1,
    capacity: 30,
    currentUsage: 8,
    usageRate: 27,
    lastMaintenance: '2025-01-10',
    nextMaintenance: '2025-02-10',
    description: '儿童游乐设施，包含滑梯、秋千、沙池等'
  },
  {
    id: 3,
    facilityName: '地下停车场',
    facilityCode: 'TC003',
    facilityType: 3,
    location: '地下车库',
    status: 1,
    capacity: 200,
    currentUsage: 156,
    usageRate: 78,
    lastMaintenance: '2025-01-08',
    nextMaintenance: '2025-02-08',
    description: '地下停车场，提供200个停车位'
  },
  {
    id: 4,
    facilityName: '社区花园',
    facilityCode: 'LH004',
    facilityType: 4,
    location: '社区中心',
    status: 2,
    capacity: null,
    currentUsage: null,
    usageRate: null,
    lastMaintenance: '2025-01-12',
    nextMaintenance: '2025-01-25',
    description: '社区绿化花园，包含草坪、花坛、休息区'
  },
  {
    id: 5,
    facilityName: '监控中心',
    facilityCode: 'AQ005',
    facilityType: 5,
    location: '物业办公室',
    status: 1,
    capacity: 5,
    currentUsage: 2,
    usageRate: 40,
    lastMaintenance: '2025-01-18',
    nextMaintenance: '2025-02-18',
    description: '社区安全监控中心，24小时监控社区安全'
  }
])

// 计算属性
const getFacilityTypeName = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: '健身设施',
    2: '娱乐设施',
    3: '停车设施',
    4: '绿化设施',
    5: '安全设施'
  }
  return typeMap[type] || '未知设施'
}

const getFacilityTypeTagType = (type: number): string => {
  const typeMap: Record<number, string> = {
    1: 'success',
    2: 'warning',
    3: 'info',
    4: 'primary',
    5: 'danger'
  }
  return typeMap[type] || 'info'
}

const getFacilityIcon = (type: number) => {
  const iconMap: Record<number, any> = {
    1: Sunny,
    2: VideoCamera,
    3: ArrowUp,
    4: House,
    5: Lock
  }
  return iconMap[type] || House
}

const getFacilityIconClass = (type: number): string => {
  return `facility-icon-${type}`
}

const getStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: '已停用',
    1: '正常使用',
    2: '维护中'
  }
  return statusMap[status] || '未知'
}

const getStatusTagType = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: 'danger',
    1: 'success',
    2: 'warning'
  }
  return statusMap[status] || 'info'
}

const getUsageRateColor = (rate: number): string => {
  if (rate >= 80) return '#F56C6C'
  if (rate >= 60) return '#E6A23C'
  if (rate >= 40) return '#409EFF'
  return '#67C23A'
}

// 方法
const handleAddFacility = () => {
  showMessage({ type: 'info', message: '新增设施功能开发中' })
}

const handleFacilityStats = () => {
  showMessage({ type: 'info', message: '设施统计功能开发中' })
}

const handleSearch = () => {
  pagination.current = 1
  loadFacilityList()
}

const handleReset = () => {
  searchForm.facilityType = undefined
  searchForm.status = undefined
  searchForm.location = ''
  pagination.current = 1
  loadFacilityList()
}

const handleExport = () => {
  showMessage({ type: 'info', message: '导出功能开发中' })
}

const handleSettings = () => {
  showMessage({ type: 'info', message: '设施设置功能开发中' })
}

const handleDetail = (row: any) => {
  currentFacility.value = row
  detailDialogVisible.value = true
}

const handleEdit = (row: any) => {
  showMessage({ type: 'info', message: `编辑设施功能开发中 - ${row.facilityName}` })
}

const handleMaintenance = (row: any) => {
  showMessage({ type: 'info', message: `设施维护功能开发中 - ${row.facilityName}` })
}

const handleEditFacility = () => {
  showMessage({ type: 'info', message: '编辑设施功能开发中' })
}

const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.current = 1
  loadFacilityList()
}

const handleCurrentChange = (current: number) => {
  pagination.current = current
  loadFacilityList()
}

const getRowClassName = ({ row }: { row: any }) => {
  if (row.status === 2) return 'maintenance-row'
  if (row.status === 0) return 'disabled-row'
  return ''
}

const loadFacilityList = () => {
  loading.value = true
  // 模拟加载数据
  setTimeout(() => {
    loading.value = false
  }, 500)
}

// 生命周期
onMounted(() => {
  loadFacilityList()
})
</script>

<style scoped lang="scss">
.facility-management-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 120px);

  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 24px;
    padding: 24px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    color: white;

    .header-left {
      .page-title {
        margin: 0 0 8px 0;
        font-size: 28px;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 12px;

        .title-icon {
          font-size: 32px;
        }
      }

      .page-description {
        margin: 0;
        font-size: 14px;
        opacity: 0.9;
      }
    }

    .header-right {
      display: flex;
      gap: 12px;
    }
  }

  .stats-cards {
    margin-bottom: 24px;

    .stats-card {
      .stats-content {
        display: flex;
        align-items: center;
        gap: 16px;

        .stats-icon {
          width: 60px;
          height: 60px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 24px;
          color: white;

          &.total {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          }

          &.normal {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
          }

          &.maintenance {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
          }

          &.disabled {
            background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
          }
        }

        .stats-info {
          .stats-number {
            font-size: 32px;
            font-weight: 700;
            color: #303133;
            line-height: 1;
          }

          .stats-label {
            font-size: 14px;
            color: #909399;
            margin-top: 4px;
          }
        }
      }
    }
  }

  .search-card {
    margin-bottom: 24px;
    border-radius: 8px;
  }

  .facility-list-card {
    border-radius: 8px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .card-title {
        font-size: 18px;
        font-weight: 600;
        color: #303133;
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }

    .facility-name {
      display: flex;
      align-items: center;
      gap: 8px;

      .facility-icon {
        font-size: 16px;
        color: #409eff;

        &.facility-icon-1 { color: #67c23a; }
        &.facility-icon-2 { color: #e6a23c; }
        &.facility-icon-3 { color: #409eff; }
        &.facility-icon-4 { color: #67c23a; }
        &.facility-icon-5 { color: #f56c6c; }
      }
    }

    .usage-rate-text {
      margin-left: 8px;
      font-size: 12px;
      color: #909399;
    }

    .text-muted {
      color: #909399;
    }

    .pagination-wrapper {
      display: flex;
      justify-content: center;
      margin-top: 24px;
    }
  }

  .facility-detail {
    .el-descriptions {
      margin-bottom: 16px;
    }
  }
}

// 表格行样式
:deep(.maintenance-row) {
  background-color: #fdf6ec;
}

:deep(.disabled-row) {
  background-color: #fef0f0;
}

// 响应式设计
@media (max-width: 768px) {
  .facility-management-container {
    padding: 12px;

    .page-header {
      flex-direction: column;
      gap: 16px;

      .header-right {
        width: 100%;
        justify-content: flex-end;
      }
    }

    .stats-cards {
      .el-col {
        margin-bottom: 16px;
      }
    }
  }
}
</style> 